﻿using MySql.Data.MySqlClient;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ScannerSuccessRate
{
    class SqlHelper
    {
        public static SqlSugarScope GetSqlInstance()
        {
            SqlSugarScope db = new SqlSugarScope(new ConnectionConfig()
            {
                //"database = 数据库名称;Data Source = 连接ip地址 ; User Id = 账号; pwd =密码;",
                // server=localhost;Database=SqlSugar4xTest;Uid=root;Pwd=haosql
                ConnectionString = "server=localhost;Database=scanner;Uid=root;Pwd=root;Convert Zero Datetime=True;Allow Zero Datetime=True;SslMode=" + MySqlSslMode.None,
                DbType = DbType.MySql,
                IsAutoCloseConnection = true //不设成true要手动close
            });

            db.Aop.OnLogExecuting = (sql, pars) => {
                Console.WriteLine(sql);
            };
            return db;
        }

        public List<Scanner> GetScanner()
        {
            SqlSugarScope db = GetSqlInstance();
            var res = db.Queryable<Scanner>().ToList();
            return res;
        }
    }


    public class Scanner 
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int id { get; set; }

        public string code { get; set; }

        public DateTime time { get; set; }

        public override string ToString()
        {
            return $"{{{nameof(id)}={id}, {nameof(code)}={code}, {nameof(time)}={time}}}";
        }
    }

}
